package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.mapper.DeptMapper;
import com.yupi.springbootinit.model.dto.UserSignRequest;
import com.yupi.springbootinit.model.entity.Dept;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.model.entity.UserSignState;
import com.yupi.springbootinit.mapper.UserSignStateMapper;
import com.yupi.springbootinit.service.DeptService;
import com.yupi.springbootinit.service.UserService;
import com.yupi.springbootinit.service.UserSignStateService;
import com.yupi.springbootinit.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (UserLoginState)表服务实现类
 *
 * @author makejava
 * @since 2024-03-07 14:11:04
 */
@Service("userLoginStateService")
public class UserSignStateServiceImpl extends ServiceImpl<UserSignStateMapper, UserSignState> implements UserSignStateService {
    @Resource
    private UserSignStateMapper userSignStateMapper;

    @Resource
    private DeptMapper deptMapper;

    @Resource
    private DeptService deptService;

    @Resource
    private UserService userService;

    /**
     * 判断全部前端做,后端只要记录就可以了
     */
    @Override
    public void sign(User user, UserSignRequest userSignRequest) {
        Integer deptId = user.getDeptId();
        Long userId = user.getId();
        String signState = userSignRequest.getSignState();
        // 签到,直接创建新的
        if (signState.equals("signIn")){
            UserSignState userSignState = new UserSignState();
            userSignState.setUserId(userId.intValue());
            userSignState.setDeptId(deptId);
            userSignState.setSignInTime(new Date());
            userSignState.setSignInLocation(userSignRequest.getDetailLocation());
            userSignState.setSignInGpsLocation(userSignRequest.getGpsLocation());
            userSignState.setLateSign(userSignRequest.getLateSign());
            userSignStateMapper.insert(userSignState);
        // 签退
        }else if (signState.equals("signOut")){
            QueryWrapper<UserSignState> userSignStateQueryWrapper = new QueryWrapper<>();
            userSignStateQueryWrapper.eq("userId",userId)
                    .eq("deptId",deptId)
                    .isNull("SignOutTime");
            UserSignState userSignState = userSignStateMapper.selectOne(userSignStateQueryWrapper);
            userSignState.setSignOutTime(new Date());
            userSignState.setSignOutGpsLocation(userSignRequest.getGpsLocation());
            userSignState.setSignOutLocation(userSignRequest.getDetailLocation());
            userSignState.setEarlySign(userSignRequest.getEarlySign());
            userSignStateMapper.updateById(userSignState);
        }
    }

    @Override
    public Map<String, Object> searchSignStateByTimePeriod(String startTime, String endTime, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate startLocalDate = LocalDate.parse(startTime, formatter);
        LocalDate endLocalDate = LocalDate.parse(endTime, formatter);
        long workdays = DateUtils.calculateWorkdays(startLocalDate, endLocalDate);
        List<Map<String,Object>>  signStateList = userSignStateMapper.selectSignStateByPeriod(startTime,endTime);
        int countEarlySign = 0;
        int countLateSign = 0;
        for (Map<String, Object> signState : signStateList) {
            int earlySignCount = 0;
            int lateSignCount = 0;
            Object earlySignObj = signState.get("earlySign");
            Object lateSignObj = signState.get("lateSign");
            if (earlySignObj != null){
                earlySignCount = Integer.parseInt(earlySignObj.toString());
            }
            if (lateSignObj != null){
                lateSignCount = Integer.parseInt(lateSignObj.toString());
            }
            countEarlySign += earlySignCount;
            countLateSign += lateSignCount;
        }
        System.out.println("工作日数量：" + workdays);
        HashMap<String, Object> map = new HashMap<>();
        map.put("shouldSign",workdays);
        map.put("alreadySign",signStateList.size());
        map.put("lateSign",countLateSign);
        map.put("earlySign",countEarlySign);
        return map;
    }

}
